#include "../tools.h"


class Solution {
public:
    int compress(vector<char>& chars) {
        int p = 0, size = chars.size();
        int left = 0, right = 0, curlen, tmp;
        while(right < size){
            if(chars[left] == chars[right]) ++right;
            else{
                curlen = right - left;
                chars[p++] = chars[left];
                if(curlen == 1){
                    
                }
                else if(curlen < 10) chars[p++] = char(curlen + '0');
                else{
                    tmp = 10;
                    while(tmp <= curlen) tmp *= 10;
                    tmp /= 10;
                    while(tmp > 0){
                        chars[p++] = char(curlen / tmp + '0');
                        curlen %= tmp;
                        tmp /= 10;
                    }
                }
                left = right;
            }
        }

        curlen = right - left;
        chars[p++] = chars[left];
        if(curlen == 1) return p;
        else if(curlen < 10) chars[p++] = char(curlen + '0');
        else{
            tmp = 10;
            while(tmp <= curlen) tmp *= 10;
            tmp /= 10;
            while(tmp > 0){
                chars[p++] = char(curlen / tmp + '0');
                cout << chars[p] << endl;
                curlen %= tmp;
                tmp /= 10;
            }
        }

        // chars[p] = '\0';
        return p;
    }
};


int main(int argc, char const *argv[])
{
    Solution sol;

    vector<char> chars = {'a', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b'};
    
    int n = sol.compress(chars);
    for(int i = 0; i < n; ++i){
        cout << chars[i] << " ";
    }
    cout << endl;

    return 0;
}
